#include    "audio_engine.h"
#include    "../common.h"

AudioEngine::AudioEngine():
fmodSystem(0),
initFlag(false)
{
    m_sounds.clear();
}

AudioEngine::~AudioEngine()
{
    if(initFlag)
    {
        shutdownAudioEngin();
    }
}

AudioEngine* AudioEngine::EnginInstance = 0;


AudioEngine* AudioEngine::getInstance()
{
    return EnginInstance;
}

void AudioEngine::createInstance()
{
    if(EnginInstance)
    {
        return;
    }

    EnginInstance = new AudioEngine();
}

void AudioEngine::releaseInstance()
{
    SAFE_DELETE_OBJ(EnginInstance);
}




bool AudioEngine::initAudioEngin()
{
    if(initFlag)
    {
        PERSONAL_DEBUG_LOG("Engine allready init!");
        return false;
    }

    FMOD_RESULT result;
    result = FMOD_System_Create(&fmodSystem);
	if(result != FMOD_OK)
	{
        PERSONAL_DEBUG_LOG("FMOD_System_Create failed!");
        return false;
    }

	result = FMOD_System_Init(fmodSystem,50,FMOD_INIT_NORMAL,0);
	if(result != FMOD_OK)
    {
        PERSONAL_DEBUG_LOG("FMOD_System_Init failed!");
        return false;
    }
    PERSONAL_DEBUG_LOG("initAudioEngin Success!");

    initFlag = true;
    return true;
}

bool AudioEngine::shutdownAudioEngin()
{
    for(auto it = m_sounds.begin(); it != m_sounds.end(); it ++)
    {
        FMOD_Sound_Release(it->second);
    }

    FMOD_System_Close(fmodSystem);
    FMOD_System_Release(fmodSystem);

    fmodSystem = 0;
    initFlag = false;
    return true;
}

bool AudioEngine::loadSound(const char* soundfile,std::string& soundName)
{
    if(initFlag == false)
    {
       PERSONAL_DEBUG_LOG("play sound without init audio engine! now init...");
       bool result =  initAudioEngin();
       if(result == false){return false;}
    }

    auto  it = m_sounds.find(soundName);
    if(it != m_sounds.end())
    {
         printf("sound [%s] allready load\n",soundName.c_str());
         return false;
    }


    FMOD_SOUND* sound = 0;
    FMOD_RESULT fresult = FMOD_System_CreateSound(fmodSystem,soundfile,FMOD_DEFAULT,0,&sound);
    if(fresult != FMOD_OK)
    {
        PERSONAL_DEBUG_LOG("FMOD_System_CreateSound failed!");
        return false;
    }

    m_sounds[soundName] = sound;

    fresult  = FMOD_Sound_SetMode(sound,FMOD_LOOP_OFF);
    if(fresult != FMOD_OK)
    {
        PERSONAL_DEBUG_LOG("FMOD_Sound_SetMode failed!");
        return true;
    }

    return true;
}

bool AudioEngine::playSound(std::string& soundName)
{
    auto  it = m_sounds.find(soundName);
    if(it == m_sounds.end())
    {
        printf("sound [%s] not find\n",soundName.c_str());
        return false;
    }

    FMOD_RESULT fresult =  FMOD_System_PlaySound(fmodSystem,it->second,0,false,0);
    if(fresult != FMOD_OK)
    {
        PERSONAL_DEBUG_LOG("FMOD_System_PlaySound failed!");
        return false;
    }

    fresult = FMOD_System_Update(fmodSystem);
    if(fresult != FMOD_OK)
    {
        PERSONAL_DEBUG_LOG("FMOD_System_Update failed!");
        return false;
    }

    return true;
}
